﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FTchina.Spider;
namespace FTchina.RSS
{
    /// <summary>
    ///caijirss 的摘要说明
    /// </summary>
    public class caijirss
    {
        public static string lanmu = string.Empty;//采集到得文章所属栏目

        public static string guizeid = string.Empty;//采集所用规则编号
        public static string moshi = string.Empty;//采集所采用的模式
        public static string regtitle = string.Empty;//标题匹配
        public static string regcontent = string.Empty;//内容匹配
        public static string regsj = string.Empty;//时间匹配
        public static string regsource = string.Empty;//来源匹配

        public static string Rssname = string.Empty;//采集目标的名称
        public static string arttitle = string.Empty;//采集文章标题
        public static string artjianjie = string.Empty;//采集文章简介
        public static string artsj = string.Empty;//采集文章录入时间

        [STAThread]
        public static void GetAllRSSArts()
        {
            DataTable dt = shujuku.GetDataTable("select distinct id from rss order by id desc");
            foreach (DataRow dr in dt.Rows)
            {
                Get1KindRss(dr["id"].ToString());//按类将所有rss采集完
            }
        }


        /// <summary>
        /// 获取指定rssid的所有文章
        /// </summary>
        /// <param name="rssid"></param>
        public static void Get1KindRss(string rssid)
        {

            GetRssGuize(rssid);//获取规则


            //取得要采集的文章列表
            DataTable dtlist = shujuku.GetDataTable("select * from rssurllist where rssid='" + rssid + "' and pass=0");
            for (int i = 0; i < dtlist.Rows.Count; i++)
            {
                artjianjie = dtlist.Rows[i]["jianjie"].ToString().Trim();
                artsj = dtlist.Rows[i]["sj"].ToString().Trim();
                arttitle = dtlist.Rows[i]["title"].ToString().Trim();

                SaveArt2DB(dtlist.Rows[i]["url"].ToString().Trim(), lanmu, artjianjie, arttitle, artsj, moshi);
            }



        }


        #region 单项采集
        /// <summary>
        /// 准备工作，得到地址、ID、栏目等信息
        /// </summary>
        public static void SingelPrepair(string rssid,string arturl)
        {

            DataTable dt = shujuku.GetDataTable("select * from rssurllist where url='" + arturl + "'");
            artjianjie = dt.Rows[0]["jianjie"].ToString().Trim();
            artsj = dt.Rows[0]["sj"].ToString().Trim();
            arttitle = dt.Rows[0]["title"].ToString().Trim();

            GetRssGuize(rssid);//获取规则
            SaveArt2DB(arturl, lanmu, artjianjie, arttitle, artsj, moshi);//采集


        }

        /// <summary>
        /// 公用的获取指定rssid的采集规则
        /// </summary>
        /// <param name="rssid"></param>
        static void GetRssGuize(string rssid)
        {
            try
            {
                DataTable dtRss = shujuku.GetDataTable("select * from rss where id=" + rssid);
                lanmu = dtRss.Rows[0]["lanmu"].ToString().Trim();
                guizeid = dtRss.Rows[0]["guizeid"].ToString().Trim();
                moshi = dtRss.Rows[0]["moshi"].ToString().Trim();
                Rssname = dtRss.Rows[0]["rssname"].ToString().Trim();
                //获取规则
                DataTable dt = shujuku.GetDataTable("select * from rssguize where id=" + guizeid, "rssguize" + guizeid);
                regtitle = dt.Rows[0]["title"].ToString().Trim();
                regcontent = dt.Rows[0]["content"].ToString().Trim();
                regsj = dt.Rows[0]["sj"].ToString().Trim();
                regsource = dt.Rows[0]["source"].ToString().Trim();
            }
            catch
            {
                return;
            }
        }

        /// <summary>
        /// 单个文章采集入库
        /// </summary>
        static void SaveArt2DB(string url, string lanmu, string jianjie, string title, string sj, string moshi)
        {
            try
            {
                string dbcontent = string.Empty;
                string dbsource = string.Empty;
                if (moshi == "1")
                {
                    GetHtml htm = new GetHtml();
                    string tmpHtml = htm.GetPage(url);
                    dbcontent = basic.SafeRequest(htm.ExecReg(tmpHtml, regcontent));
                    dbcontent = basic.DealScript(dbcontent);
                    dbsource = basic.SafeRequest(htm.ExecReg(tmpHtml, regsource));
                    dbcontent +="<br/><br/><a href=" + url + " target=_blank>[网友投稿]</a>";

                }
                else
                {
                    dbcontent = basic.SafeRequest(jianjie) + "<br/><br/><a href=" + url + " target=_blank>[网友投稿]</a>";
                  //  dbcontent = basic.SafeRequest(jianjie);
                    dbsource = Rssname;

                }
                string sql = "insert into content (lanmuid,title,data,content,author,source,jianjie,ispass) values ('" + lanmu + "','" + title + "','" + sj + "','" + dbcontent + "','" + dbsource + "','" + dbsource + "','" + jianjie + "','1')";
                string RemoveListSql = "update rssurllist set pass='1' where url='" + url + "'";

                if (dbcontent.Trim().Length != 0)
                {
                    shujuku.ExecuteSql(sql);
                }
                shujuku.ExecuteSql(RemoveListSql);
                if (basic.IsHtml) FTchina.Api.htmlpath.MakeNewHtmlByBiggestID();
            }
            catch { }
        }

        #endregion

    }
}